<template>
  <el-dialog :title="isEdit ? '编辑配置' : '新增配置'" v-model="visible" width="500px" @close="handleClose">
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="配置键" prop="key">
        <el-input v-model="form.key" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="配置值" prop="value">
        <el-input v-model="form.value" type="textarea" :rows="3" />
      </el-form-item>
      
      <el-form-item label="配置名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="配置类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%">
          <el-option label="字符串" value="string" />
          <el-option label="数字" value="number" />
          <el-option label="布尔值" value="boolean" />
          <el-option label="JSON" value="json" />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" type="textarea" :rows="2" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed, watch } from "vue";
import { ElMessage } from "element-plus";
import { createConfig, updateConfig } from "@/api/config";

const props = defineProps({
  modelValue: Boolean,
  configData: Object,
});

const emit = defineEmits(["update:modelValue", "success"]);

const formRef = ref();
const loading = ref(false);

const form = reactive({
  id: null,
  key: '',
  value: '',
  name: '',
  description: '',
  type: 'string',
  status: true
});

const rules = {
  key: [{ required: true, message: "请输入配置键", trigger: "blur" }],
  value: [{ required: true, message: "请输入配置值", trigger: "blur" }],
  name: [{ required: true, message: "请输入配置名称", trigger: "blur" }],
  type: [{ required: true, message: "请选择配置类型", trigger: "change" }],
};

const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit("update:modelValue", val),
});

const isEdit = computed(() => !!props.configData);

const resetForm = () => {
  Object.assign(form, {
    id: null,
    configKey: "",
    configValue: "",
    configName: "",
    remark: "",
    status: true,
  });
  formRef.value?.clearValidate();
};

const handleClose = () => {
  visible.value = false;
  resetForm();
};

const handleSubmit = async () => {
  if (!formRef.value) return;

  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        if (isEdit.value) {
          await updateConfig(form.id, form);
          ElMessage.success("更新成功");
        } else {
          await createConfig(form);
          ElMessage.success("创建成功");
        }
        emit("success");
        handleClose();
      } catch (error) {
        ElMessage.error(error.message || "操作失败");
      } finally {
        loading.value = false;
      }
    }
  });
};

watch(
  () => props.configData,
  (newVal) => {
    if (newVal) {
      Object.assign(form, newVal);
    } else {
      resetForm();
    }
  },
  { immediate: true }
);
</script>